﻿var userList="/manager/admin/userList.do";
var getContextPath="/manager/admin/userDelete.do";
var columns = [[
    {field:'name',title:'用户名',width:50},
    {field:'tel',title:'电话',width:100},
    {field:'email',title:'邮箱',width:100},
    {field:'type',title:'数据库',width:100,formatter: function (value, row, index) {
        switch(value) {
            case 1 : return '内部数据库';
            // case 2 : return '山东通用户';
        }
        return '未知';}},
    {field:'status',title:'状态',width:100,formatter: function (value, row, index) {
        switch(value) {
            case 1 : return '正常';
            case 2 : return '停止';
            case 3 : return '已删除';
        }
        return '未知';}}
]];

function query(curPage, pageSize){
	
    curPage = parseInt(curPage);
    pageSize = parseInt(pageSize);

    var name=$("#name").val();

	$.get(userList, {"name":name,"page": curPage, "pageSize": pageSize}, function (json) {
		var code=json.code;
    	if(code!=200){
    	  $('#tt').datagrid({data:[]});
    	  return;
    	}
		var data = json.data;
		var total = data.size;
		var totalPage = Math.ceil(total / pageSize);
		var start = (curPage - 1) * pageSize;
		var dataLength = 0;
		data = data["results"];
		if (data != null) {
			dataLength = data.length;
		}
		var iHeight = document.documentElement.clientHeight;
        $('#tt').datagrid( {
            width : '100%',
            height : iHeight-85,
            fitColumns : true,
            columns : columns,
            singleSelect:true,//是否单选 
            striped:true,//表格奇偶渐变色
            pagination: true,//打开翻页功能
            toolbar: CreateToolBar(),
            pageList: [30,50,100],
            data:data
        });
        
	    var p = $('#tt').datagrid('getPager');
	    $(p).pagination({
		      total: total,
		      pageList: [30, 50, 100, 500, 1000],
		      pageSize: pageSize,
		      pageNumber: curPage,
		      beforePageText: '第',
		      afterPageText: '页    共 ' + totalPage + ' 页',
		      displayMsg: '当前显示 ' + (start + 1) + '-' + (start + dataLength) + ' 条记录 共 ' + total + ' 条记录',
		      onSelectPage: function (curPage, pageSize) {
		        query(curPage, pageSize,true);
		      },
		      onChangePageSize: function (ps) {
		        pageSize = ps;
		      }
	    });
	}, "json");
}
$(document).ready(function () {
	$('#fmAdd').form( {
		success : function(data) {
		    CRUD.queryList();
		    try{
			    var result = eval('(' + data + ')');
			    if(result == true){
			    	$('#dialogAdd').dialog('close');
				 } else {
			        $.messager.alert('提示', '保存失败');
				 }
			}catch(err){
			}
		}
	});

    CRUD.queryList();
    InitForm();

    //确定添加
    $('#a_AddOK').click(function () {
        if (!document.getElementById("userName").value) {
            $.messager.alert('提示', '用户名不能为空！');
            return;
        }
        if (!$('#password').val()) {
            $.messager.alert('提示', '密码不能为空！');
            return;
        }
        if (!$('#email').val()) {
            $.messager.alert('提示', '邮箱不能为空！');
            return;
        }
        if (!$('#status').combobox('getValue')) {
            $.messager.alert('提示', '状态不能为空！');
            return;
        }
        $('#fmAdd').submit();
    });

    //取消
    $('#a_AddCancle').click(function () {
    	var textarea = $('#idScales');
    	if (textarea) {
    		textarea.remove();
    	}
        $('#fmAdd').form('clear');
        $('#dialogAdd').dialog('close');
        $('#tt').datagrid("clearSelections");
    });
});

var CRUD = {
    queryList: function () {
    	 query(1, 30)
    },

    Add: function () {
    	$('#fmAdd').form('clear');
    	var textarea = $('#idScales');
    	if (textarea) {
    		textarea.remove();
    	}
        $('#dialogAdd').dialog('open');
        $('#tt').datagrid("clearSelections");
        $('#dialogAdd').dialog("setTitle", "添加用户");
    },
    Del: function () {
        var r = $('#tt').datagrid('getSelected');
        if (r) {
            $.messager.confirm('删除提示', '你确认删除这一条数据吗？', function (d) {
                if (d) {
                    $.get(getContextPath,{"id":r.id}, function (data) {
                        if (data == 'true') {
                            $.messager.show(
                                {
                                    title: '系统提示',
                                    msg: '删除成功！',
                                    timeout: 1500
                                });
                            $('#tt').datagrid("clearSelections");
                            CRUD.queryList();
                        } else {
                            $.messager.alert('错误', '删除失败！');
                        }
                    });
                }
            });
        } else {
            $.messager.alert('提示', '请选择一行数据!');
        }
    },
    Update: function () {
        var r = $('#tt').datagrid('getSelected');
        if (r == null) {
            $.messager.alert('提示', '请选择一行数据');
            return;
        }
        $('#dialogAdd').dialog('open');
        $('#fmAdd').form('load', {
            userName: r.name,
            tel: r.tel,
            email: r.email
        });

        if (r.ruleRegionType == 1) {
        	$('#scaleAll').attr('checked', 'checked');
        } else if (r.ruleRegionType == 2) {
        	$('#scaleUserDefined').attr('checked', 'checked');
        	var textarea = document.getElementById('idScales');
        	if (!textarea) {
        		$('#scaleTd').append($('<textarea style="display:block" id="idScales" cols ="50" rows = "3"></textarea>'));
        	}
        	$('#idScales').val(r.ruleRegion);
        }
        $('#dialogAdd').dialog("setTitle", "修改用户");
    },
    ViewRoles: function () {
        var r = $('#tt').datagrid('getSelected');
        if (r == null) {
            $.messager.alert('提示', '请选择一行数据');
            return;
        }
        $('#roles').dialog('open');
        showHavaRoles(r.id);
        $('#roles').dialog("setTitle", "查看修改用户角色");
    },
    ViewPs: function () {
        var r = $('#tt').datagrid('getSelected');
        if (r == null) {
            $.messager.alert('提示', '请选择一行数据');
            return;
        }
        $('#permissions').dialog('open');
        showHavaPermissions(r.id);


        $('#permissions').dialog("setTitle", "查看修改用户权限");
    }
};

function CreateToolBar() {
    var a = [];
    var isReflesh = true;
    var isAdd = true;
    var isDel = true;
    var isUpdate = true;
    var isViewRoles = true;
    var isViewPermissions = true;
    if (isAdd) {
        a.push({
            text: '添加',
            iconCls: 'icon-add',
            handler: function () {
                CRUD.Add();
            }
        });
        a.push('-');
    }

    if (isUpdate) {
        a.push({
            text: '编辑',
            iconCls: 'icon-edit',
            handler: function () {
                CRUD.Update();
            }
        });
        a.push('-');
    }

    if (isDel) {
        a.push({
            text: '删除',
            iconCls: 'icon-cut',
            handler: function () {
                CRUD.Del();
            }
        });
        a.push('-');
    }

    if (isViewRoles) {
        a.push({
            text: '查看修改用户角色',
            iconCls: 'icon-search',
            handler: function () {
                CRUD.ViewRoles();
            }
        });
        a.push('-');
    }

    if (isViewPermissions) {
        a.push({
            text: '查看修改用户权限',
            iconCls: 'icon-search',
            handler: function () {
                CRUD.ViewPs();
            }
        });
        a.push('-');
    }
    return a;
}

function InitForm() {
    var width = 800;
    var height = window.screen.height;
    if (height > 600) {
        height = 600;
    }
    $('#dialogAdd').dialog(
            {
                closed: true,
                title: "添加用户",
                width: 450,
                height: 250
            });
    $('#roles').dialog(
        {
            closed: true,
            title: "查看修改用户角色",
            width: 750,
            height: 550
        });
    $('#permissions').dialog(
        {
            closed: true,
            title: "查看修改用户权限",
            width: 750,
            height: 550
        });
}

//自适应
window.onresize = function () {
	var iHeight = document.documentElement.clientHeight;
	$("#tt").datagrid('resize',{height:iHeight-85+'px'});            
}
